package org.qing.cloud.common.security.component;

import org.qing.cloud.common.core.constant.CommonConstants;
import org.qing.cloud.common.security.exception.SysAuth2Exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import lombok.SneakyThrows;

/**
 * OAuth2 异常格式化
 */
public class SysAuth2ExceptionSerializer extends StdSerializer<SysAuth2Exception> {

	private static final long serialVersionUID = -2091106368115084433L;

	public SysAuth2ExceptionSerializer() {
		super(SysAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(SysAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
		gen.writeStartObject();
		gen.writeObjectField("code", CommonConstants.FAIL);
		gen.writeStringField("msg", value.getMessage());
		gen.writeStringField("data", value.getErrorCode());
		gen.writeEndObject();
	}

}
